﻿using System;
using System.Runtime.Serialization;
using Newtonsoft.Json;

namespace OrkutAPILibrary.Entities
{
    /// <summary>
    /// The Orkut Exception Class
    /// </summary>
    [Serializable]
    public class OrkutException : Exception
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="OrkutException"/> class
        /// </summary>
        /// <param name="message">The message.</param>
        public OrkutException(string message)
            : base(message)
        {

        }

        /// <summary>
        /// Initializes a new instance of the <see cref="OrkutException"/> class.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="innerException">The inner exception.</param>
        public OrkutException(string message, Exception innerException)
            : base(message, innerException)
        {

        }

        /// <summary>
        /// Initializes a new instance of the <see cref="OrkutException"/> class.
        /// </summary>
        /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
        /// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext"/> that contains contextual information about the source or destination.</param>
        /// <exception cref="T:System.ArgumentNullException">The <paramref name="info"/> parameter is null. </exception>
        /// <exception cref="T:System.Runtime.Serialization.SerializationException">The class name is null or <see cref="P:System.Exception.HResult"/> is zero (0). </exception>
        public OrkutException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {

        }

        private bool isServerError = false;
        private bool isClientError = false;

        /// <summary>
        /// Gets or sets the error code.
        /// </summary>
        /// <value>The error code.</value>
        [JsonProperty(PropertyName = "code")]
        public int ErrorCode { get; set; }

        /// <summary>
        /// Gets or sets the error message.
        /// </summary>
        /// <value>The error message.</value>
        [JsonProperty(PropertyName = "message")]
        public string ErrorMessage { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is client error.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is client error; otherwise, <c>false</c>.
        /// </value>
        public bool IsClientError
        {
            get { return isClientError; }
            set 
            {
                if (ErrorCode >= 400 && ErrorCode < 500)
                    isClientError = true;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is server error.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is server error; otherwise, <c>false</c>.
        /// </value>
        public bool IsServerError
        {
            get { return isServerError; }
            set 
            {
                if (ErrorCode >= 500 && ErrorCode < 600)
                    isServerError = true;
            }
        }
    }
}
